package com.byxy.shop.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.byxy.shop.dao.GoodsCategoryDao;
import com.byxy.shop.dao.GoodsInfoDao;
import com.byxy.shop.entity.GoodsCategory;
import com.byxy.shop.entity.GoodsInfo;
import com.byxy.shop.service.GoodsSearchService;
import com.byxy.ssm.shop.vo.GoodsDetailVO;
import com.byxy.ssm.shop.vo.SearchCategoryVO;
import com.byxy.ssm.shop.vo.SearchPageCategoryVO;
import com.github.pagehelper.PageHelper;

@Service
@Transactional
public class GoodsSearchServiceImpl implements GoodsSearchService {

	@Autowired
	private GoodsInfoDao goodsInfoDao;

	@Autowired
	private GoodsCategoryDao goodsCategoryDao;

	@Override
	public List<GoodsDetailVO> findByCategoryIdAndKeyword(Integer goodsCategoryId, String keyword, Integer currPage,
			Integer rows, String orderBy) {
//		if(goodsCategoryId == null) {
//			List<GoodsInfo> infos = goodsInfoDao.findBykeyword("%"+keyword+"%");// keyword == null ? null:
//			List<GoodsDetailVO> goods = new ArrayList<>();
//			for(GoodsInfo info : infos) {
//				GoodsDetailVO good = new GoodsDetailVO();
//				BeanUtils.copyProperties(info, good);
//				goods.add(good);
//			}
//			return goods;
//		}else {
//		List<GoodsInfo> infos = goodsInfoDao.findByCategoryId(goodsCategoryId);
//		List<GoodsDetailVO> goods = new ArrayList<>();
//		for(GoodsInfo info : infos) {
//			GoodsDetailVO good = new GoodsDetailVO();
//			BeanUtils.copyProperties(info, good);
//			goods.add(good);
//		}
//		return goods;
//		}

		if (orderBy == null || orderBy.equalsIgnoreCase("default")) {
			PageHelper.orderBy("goods_id");
		} else if (orderBy.equalsIgnoreCase("new")) {
			PageHelper.orderBy("create_time DESC");
		} else if (orderBy.equalsIgnoreCase("price")) {
			PageHelper.orderBy("original_price");
		}

		PageHelper.startPage(currPage, rows);
		List<GoodsInfo> infos = goodsInfoDao.findByCategoryIdAndkeyword(goodsCategoryId,
				keyword == null ? null : "%" + keyword + "%");// keyword == null ? null:
		List<GoodsDetailVO> goods = new ArrayList<>();
		for (GoodsInfo info : infos) {
			GoodsDetailVO good = new GoodsDetailVO();
			BeanUtils.copyProperties(info, good);
			goods.add(good);
		}
		return goods;
	}

	@Override
	public int getTotalByCategoryIdAndKeyword(Integer goodsCategoryId, String keyword) {
		return goodsInfoDao.countByCategoryIdAndkeyword(goodsCategoryId, keyword == null ? null : "%" + keyword + "%");
//		return 0;
	}

	@Override
	public SearchPageCategoryVO getSearchPageCategoryVO(Integer goodsCategoryId) {

		SearchPageCategoryVO searchPageCategoryVO = new SearchPageCategoryVO();
		GoodsCategory category = goodsCategoryDao.getByCategoryId(goodsCategoryId);
		if (category != null && category.getCategoryLevel() == 3) {
			// 当前分类
			searchPageCategoryVO.setCurrentCategoryName(category.getCategoryName());

			// 获取上级分类
			GoodsCategory categoryParent = goodsCategoryDao.getByCategoryId(category.getParentId());
			searchPageCategoryVO.setSecondLevelCategoryName(categoryParent.getCategoryName());

			if (categoryParent != null && categoryParent.getCategoryLevel() == 2) {
				List<GoodsCategory> categoryList = goodsCategoryDao
						.findCategoryByLevelAndId((int) category.getCategoryLevel(), category.getParentId());
				List<SearchCategoryVO> thirdLevelCategory = new ArrayList<>();
				for (GoodsCategory c : categoryList) {
					SearchCategoryVO searchCategoryVO = new SearchCategoryVO();
					BeanUtils.copyProperties(c, searchCategoryVO);
					thirdLevelCategory.add(searchCategoryVO);
				}
				searchPageCategoryVO.setThirdLevelCategoryList(thirdLevelCategory);
			}
		}

		return searchPageCategoryVO;
	}

}
